﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Xml;
using System.Windows.Forms;
using Javavaj.iHunter;
using Javavaj.iHunter.Core;


namespace Javavaj.iHunter.ThuLearnChannels
{
    /// <summary>
    /// 用于创建网络学堂频道对象
    /// </summary>
    public partial class ThuLearnChannelFactory : IChannelFactory
    {
        private IList<KeyValuePair<string, string>> courselist = null;
        private string username = "";
        private string password = "";
        private XmlDocument settings = null;

        /// <summary>
        /// 构造函数
        /// </summary>
        public ThuLearnChannelFactory()
        {
            courselist = new List<KeyValuePair<string, string>>();
        }


        #region IChannelCreator Members

        /// <summary>
        /// 获取频道
        /// </summary>
        /// <returns>网络学堂各门课对应的频道</returns>
        public IEnumerable<IChannel> ListChannels()
        {
            foreach (KeyValuePair<string, string> pair in courselist)
            {
                yield return new ThuLearnChannel(pair.Key, pair.Value, username, password);
            }
            yield break;
        }

        /// <summary>
        /// 初始化频道配置
        /// </summary>
        /// <param name="settings">配置文件</param>
        public void InitSettings(System.Xml.XmlDocument sts)
        {
            /* The Xml setting document looks like this:
             * <Settings>
             *     <Username>
             *         #@$@$@#$@$
             *     </Username>
             *     <Password>
             *         #%#@%@#%#$% 
             *     </Password>
             *     <Course>
             *         <Chosen>true/false</Chosen>
             *         <Id>123345</Id>
             *         <Title>#@$</Title>
             *     </Course>
             *     <Course>
             *         <Chosen>true/false</Chosen>
             *         <Id>123345</Id>
             *         <Title>#@$</Title>
             *     <Course>
             * <Settings>
             */

            courselist.Clear();
            settings = sts;

            try
            {
                //get username & password
                username = settings.DocumentElement["Username"].InnerText;
                password = settings.DocumentElement["Password"].InnerText;
                //get courseId
                //
                foreach (XmlElement ele in settings.DocumentElement.GetElementsByTagName("Course"))
                {
                    if (bool.Parse(ele["Chosen"].InnerText) == true)
                        courselist.Add(new KeyValuePair<string, string>(ele["Id"].InnerText, ele["Title"].InnerText));
                }
            }
            catch
            {
                //MessageBox.Show("插件配置错误。请重新设置。", "网络学堂");
                courselist.Clear();
            }
            return;
        }

        /// <summary>
        /// 显示插件配置界面
        /// </summary>
        public void Config()
        {
            XmlDocument newsettings = null;
            ConfigDialog cd = new ConfigDialog(settings);
            while (true)
            {
                cd.ShowDialog();
                if (cd.DialogResult == DialogResult.OK && cd.NewSettings != null)
                {
                        newsettings = cd.NewSettings;
                        settings = cd.NewSettings;

                        username = settings.DocumentElement["Username"].InnerText;
                        password = settings.DocumentElement["Password"].InnerText;
                        courselist.Clear();
                        foreach (XmlElement node in settings.DocumentElement.GetElementsByTagName("Course"))
                        {
                            if (bool.Parse(node["Chosen"].InnerText) == true)
                            {
                                courselist.Add(new KeyValuePair<string, string>(node["Id"].InnerText, node["Title"].InnerText));
                            }
                        }

                        if (SettingChanged != null)
                            SettingChanged.Invoke(this, new PluginSettingChangedEventArgs(this, newsettings));
                    break;
                }
                else if (cd.DialogResult == DialogResult.Cancel)
                    break;
            }
            cd.Close();
        }

        /// <summary>
        /// 插件设置更改
        /// </summary>
        public event PluginSettingChangedEventHandler SettingChanged;

        #endregion
    }
}
